package com.pub.util;

import java.util.HashSet;

public class WordUtils
{
	
	public static HashSet<String> singleWordDic = new HashSet<String>(100);
    public static HashSet<String> singleAddSDic = new HashSet<String>(100);
    
    public static int iGetWordCountWithSpace(String strData)
	{
		if(strData == null)
			return 0;
		String[] arrWord = strData.split(" ");
		
		return arrWord.length;
	}
	
	public static int iGetWordCountWithChar(String strData)
	{
		if(strData == null)
			return 0;
		int iLen = strData.length();
		int iCount = 0;
		for(int i = 0; i < iLen; i++){
			if(strData.charAt(i) == ' ')
				iCount++;
		}
				
		return iCount == 0 ? 0 : iCount + 1;
	}
	
	public static String strDelAdditionalSpace(String strData)
	{
		if(strData == null)
			return null;
		
		while(true){
			if(strData.indexOf("  ") == -1){
				break;
			}else
				strData = strData.replaceAll("  "," ");
		}
		
		return strData;
	}
	
   public static void vdSetSingularWordDic()
    {
        singleWordDic.add("bus");
        singleWordDic.add("physics");
        singleWordDic.add("maths");
        singleWordDic.add("politics");
        singleWordDic.add("news");
        singleWordDic.add("goods");
        singleWordDic.add("stairs");
        singleWordDic.add("dress");
        
        singleAddSDic.add("boy");
    }
    
    public static String singularAndPluralConvert(String strData)
    {
    	if(isPlural(strData)){
    		return PluralToSingular(strData);
    	}else{
    		return singularToPlural(strData);
    	}
    }
    
    public static boolean isPlural(String strData)
    {
        if(singleWordDic.contains(strData))
        {
           return false;
        } else if(strData.endsWith("s")) {
            return true;
        } else {
            return false;
        }
    }
    
    public static String singularToPlural(String strData)
    {
    	strData = strData.toLowerCase();
    	
    	if(isPlural(strData)) {
            return strData;
    	}else if(strData.length() < 3){
    		return strData;
    	} else if(strData.endsWith("f")) {
            return strData.substring(0, strData.length()-1) + "ves";
        } else if(strData.endsWith("fe")) {
            return strData.substring(0, strData.length()-2) + "ves";
        } else if(strData.toLowerCase().endsWith("y")) {
            if( strData.charAt(strData.length()-2) == 'u' || 
                strData.charAt(strData.length()-2) == 'o' ||
                strData.charAt(strData.length()-2) == 'i' || 
                strData.charAt(strData.length()-2) == 'e' ||
                strData.charAt(strData.length()-2) == 'a' )
            {
                return strData + "s";
            } else {    
                return strData.substring(0, strData.length()-1) + "ies";
            }
        } else if(strData.endsWith("ss") ||  strData.endsWith("s") || strData.endsWith("x") || strData.endsWith("sh") || strData.endsWith("ch")) {
            return strData + "es";
        } else {
            return strData + "s";
        }
    }
    
    public static String PluralToSingular(String strData)
    {
    	strData = strData.toLowerCase();
    	
    	if(!isPlural(strData)) {
            return strData;
    	}else if(strData.length() < 3){
    		return strData;
        } else if(singleAddSDic.contains(strData)) {
    		return strData + "s";
    	} else if(strData.endsWith("ves")) {
            return strData.substring(0, strData.length()-3) + "f";
        } else if(strData.endsWith("ies")) {
            return strData.substring(0, strData.length()-3) + "y";
        } else if(strData.endsWith("es")) {
            return strData.substring(0, strData.length()-2) ;
        } else if(strData.endsWith("s")) {
            return strData.substring(0, strData.length()-1) ;
        } else {
            return strData.substring(0, strData.length()-1) ;
        }
    }
    
    
    public static void main(String[] args)
    {
        //putSingleWordDic();
        //System.out.println(MoreToSingle("buses"));
       // System.out.println(MoreToSingle("cars"));
       // System.out.println(MoreToSingle("boys"));
        //System.out.println(MoreToSingle("wives"));
        //System.out.println(MoreToSingle("families"));
    	
        
        
    }
}